let UserDataManager = require('UserDataManager')
let GameDataManager = require('GameDataManager')
let Menu = require('Menu')
let WX = require('WX')
let AudioManager = require('AudioManager')

let CarShop = cc.Class({
  extends: cc.Component,

  properties: {
    Car: cc.Prefab,

    carContent: cc.Node,
    ui: cc.Node,

    cashTxt: cc.Label,

    scrollView: cc.Node
  },

  statics: {
    instance: null
  },

  ctor () {
    if (!CarShop.instance) {
      CarShop.instance = this
    }
  },

  onEnable () {
    this.updateCash()

    setTimeout(()=>{
      if(this.carContent){
        this.carContent.runAction(
          cc.sequence(
            cc.moveTo(1.5, 0, this.carLevel * 180),
            cc.moveBy(1, 0, -100)
          )
        )
      }
    },200)

  },
  onLoad () {
    console.log('carShop-onLoad')
    this.initCarContent()
  },

  updateCash () {
    console.log('CarShop-updateCash')
    // this.cashTxt.string = "￥"+UserDataManager.getUserData().dollar;
    this.cashTxt.string = UserDataManager.getUserData().dollar
  },

  initCarContent () {
    console.log('CarShop-initCarContent')
    let carTotalNum = 43
    this.carLevel = UserDataManager.getUserData().carLevel
    let preCar = null
    for (let i = 0; i < carTotalNum; i++) {
      let car = cc.instantiate(this.Car)
      car.removeComponent(cc.RigidBody)
      car.removeComponent(cc.PhysicsPolygonCollider)
      car.removeComponent(cc.BoxCollider)

      if (preCar) preCar.nextCar = car
      preCar = car

      let activeCar = car.getChildByName('car_' + i)

      let priceTag = car.getChildByName('priceTag')

      let subText = car.getChildByName('subText')

      let unknowCar = car.getChildByName('unknowCar')

      let carPartical = car.getChildByName('carPartical')

      let clickUnlock = car.getChildByName('click_unlock')

      car._isGoods = true
      car._activeCar = activeCar
      car._priceTag = priceTag
      car._subText = subText
      car._unknowCar = unknowCar
      car._carPartical = carPartical
      car._clickUnlock = clickUnlock
      car._price = GameDataManager.getCarPrice(i)

      // subText.getChildByName("value").getComponent(cc.Label).string = "￥" + GameDataManager.getCarValue(i);
      subText
        .getChildByName('value')
        .getComponent(cc.Label).string = GameDataManager.getCarValue(i)

      if (i <= this.carLevel) {
        activeCar.active = true
        priceTag.active = false
        subText.active = true
        unknowCar.active = false
        carPartical.active = false
        clickUnlock.active = false
      } else {
        priceTag.getChildByName('txt').getComponent(cc.Label).string =
          car._price
        subText.active = false
        unknowCar.active = true
        carPartical.active = false

        if (i == this.carLevel + 1) this.setTabFunc(car)
      }
      this.carContent.addChild(car)
    }
  },

  setTabFunc (car) {
    let buyCarSuccess = () => {
      WX.ald.sendEventBuyCar(this.carLevel, UserDataManager.getUserData().dollar, car._price)
      UserDataManager.saveUserData2('carLevel', this.carLevel)
      car.stopAllActions()
      car.scale = 1
      car._activeCar.active = true
      car._unknowCar.active = false
      car._subText.active = true
      car._priceTag.active = false
      car._clickUnlock.active = false;
      car.off(cc.Node.EventType.TOUCH_START)
      this.setTabFunc(car.nextCar)
      this.updateCash()
    }
    if(car){
      car.runAction(
        cc.repeatForever(cc.sequence(cc.scaleTo(0.4, 1.05), cc.scaleTo(0.3, 1)))
      )
    }
    car._clickUnlock.active = true;
    car.on(
      cc.Node.EventType.TOUCH_START,
      () => {
        let curDollar = UserDataManager.getUserData().dollar
        console.log('This car cost $' + car._price)
        if (curDollar >= car._price) {
          this.carLevel++
          UserDataManager.saveUserData2('dollar', curDollar - car._price)
          buyCarSuccess()
        } else {
          console.log('Money is not enough!')
          WX.share(
            () => {
              // success
              //AudioManager.instance.play('bgm')
              buyCarSuccess()
            },
            () => {
              //AudioManager.instance.play('bgm')
              console.log('Fail to share!')
            }
          )
        }
      },
      this
    )
  },

  start () {},

  open () {
    this.node.active = true
    this.ui.active = false
  },

  close () {
    this.node.active = false
    this.ui.active = true
    Menu.instance.updateDollarLabel()
    Menu.instance.updateCarBtn()
  },

  update (dt) {
  }
})
